<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>达达微博</title>
    <!-- import CSS -->
    <link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.15.9/theme-chalk/index.css">
    <style>        body {
        font-family: Arial, sans-serif;
        margin: 0;
        padding: 0;
        background-color: #f5f7fa;
    }
    #app {
        max-width: 800px;
        margin: 0 auto;
        padding: 20px;
        background-color: white;
        border-radius: 8px;
        box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
    }
    h1 {
        text-align: center;
        color: #333;
    }
    .welcome-message {
        text-align: center;
        margin-bottom: 20px;
    }
    .links {
        display: flex;
        justify-content: space-around;
        margin-bottom: 20px;
    }
    .links a {
        text-decoration: none;
        color: #409EFF;
        font-size: 16px;
    }
    .post-list {
        list-style-type: none;
        padding: 0;
    }
    .post-item {
        border-bottom: 1px solid #ebeef5;
        padding: 10px 0;
    }
    .post-item:last-child {
        border-bottom: none;
    }
    .post-item a {
        text-decoration: none;
        color: #333;
        font-size: 16px;
    }
    </style>
</head>
<body>

<div id="app">
    <h1>微博首页</h1>

    <!-- 判断是否登录，显示不同的内容 -->
    <div v-if="isLogin" class="welcome-message">
        <h3>欢迎 <span style="color: darkred">{{ user.nickname }}</span> 回来！</h3>
        <a href="/insert.html">发微博</a>
        <a href="javascript:void(0)" @click="logout()">退出登录</a>
    </div>
    <div v-else class="links">
        <a href="/reg.html">注册</a>
        <a href="/login.html">登录</a>
    </div>

    <hr>

    <!-- 需要有一个数组，数组中存放的是微博对象 -->
    <ul class="post-list">
        <li v-for="w in arr" class="post-item">
            <a :href="'/detail.html?id=' + w.id">
                <span style="color: darkred">{{ w.nickname }}</span>: {{ w.content }}
            </a>
        </li>
    </ul>
</div>

<!-- 引入axios和vue的框架文件 -->
<script src="https://cdn.bootcss.com/axios/0.18.0/axios.min.js"></script>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
<script>    let v = new Vue({
    el: "#app",
    data: {
        isLogin: false,
        user: {},
        arr: []
    },
    methods: {
        logout() {
            axios.get("/v1/users/logout").then(function (response) {
                v.isLogin = false;
            });
        }
    },
    created: function () {
        // 进入到页面就要发请求，获取当前登录的用户对象
        axios.get("/v1/users/currentUser").then(function (response) {
            // 如果登录过得到的response.data是一个UserVO对象，未登录得到的是一个空字符串""
            // 此处给user属性赋值为UserVO
            v.user = response.data;
            if (response.data === "") { // 没有拿到当前登录的对象
                v.isLogin = false;
            } else { // 得到了当前登录的对象
                v.isLogin = true;
            }
        });

        // 发请求获取所有微博数据
        axios.get("/v1/weibo/selectIndex").then(function (response) {
            v.arr = response.data;
        });
    }
});
</script>

</body>
</html>